﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebMVC.Controllers
{
    public class KhoHangController : Controller
    {
        databaseDataContext db = new databaseDataContext();
        //
        // GET: /KhoHang/
        [Authorize(Roles = "0")]
        public ActionResult Index()
        {
            ViewData["Items"] = from h in db.KhoHangs
                                select h;
            return View();
        }

        //
        // GET: /KhoHang/Details/5
        [Authorize(Roles = "0")]
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /KhoHang/Create
        [Authorize(Roles = "0")]
        public ActionResult Create()
        {
            IEnumerable<SelectListItem> listChiNhanh = db.ChiNhanhs.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listLoaiKhoHang = db.LoaiKhoHangs.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            ViewData["ListChiNhanh"] = listChiNhanh;
            ViewData["ListLoaiKhoHang"] = listLoaiKhoHang;
            return View();
        }

        //
        // POST: /KhoHang/Create
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Create(KhoHang khohang)
        {
            try
            {
                // TODO: Add insert logic here
                khohang.Ngay_cap_nhat_cuoi = DateTime.Now;
                db.KhoHangs.InsertOnSubmit(khohang);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                IEnumerable<SelectListItem> listChiNhanh = db.ChiNhanhs.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listLoaiKhoHang = db.LoaiKhoHangs.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                ViewData["ListChiNhanh"] = listChiNhanh;
                ViewData["ListLoaiKhoHang"] = listLoaiKhoHang;
                return View();
            }
        }

        //
        // GET: /KhoHang/Edit/5
        [Authorize(Roles = "0")]
        public ActionResult Edit(int id)
        {
            KhoHang model = (from m in db.KhoHangs
                             where m.ID == id
                             select m).SingleOrDefault();
            IEnumerable<SelectListItem> listChiNhanh = db.ChiNhanhs.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            IEnumerable<SelectListItem> listLoaiKhoHang = db.LoaiKhoHangs.Select(c => new SelectListItem
            {
                Value = c.ID.ToString(),
                Text = c.Ten.ToString()
            });

            ViewData["ListChiNhanh"] = listChiNhanh;
            ViewData["ListLoaiKhoHang"] = listLoaiKhoHang;
            return View(model);
        }

        //
        // POST: /KhoHang/Edit/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Edit(int id, KhoHang khohang)
        {
            KhoHang model = (from m in db.KhoHangs
                             where m.ID == id
                             select m).SingleOrDefault();
            try
            {
                // TODO: Add update logic here
                model.Ten = khohang.Ten;
                model.Ma_chi_nhanh = khohang.Ma_chi_nhanh;
                model.Ma_loai_kho_hang = khohang.Ma_loai_kho_hang;
                model.Ghi_chu = khohang.Ghi_chu;
                model.Ngay_cap_nhat_cuoi = DateTime.Now;
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                IEnumerable<SelectListItem> listChiNhanh = db.ChiNhanhs.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                IEnumerable<SelectListItem> listLoaiKhoHang = db.LoaiKhoHangs.Select(c => new SelectListItem
                {
                    Value = c.ID.ToString(),
                    Text = c.Ten.ToString()
                });

                ViewData["ListChiNhanh"] = listChiNhanh;
                ViewData["ListLoaiKhoHang"] = listLoaiKhoHang;
                return View(model);
            }
        }

        //
        // GET: /KhoHang/Delete/5
        [Authorize(Roles = "0")]
        public ActionResult Delete(int id)
        {
            var model = (from h in db.KhoHangs
                         where h.ID == id
                         select h).Take(1).SingleOrDefault();
            db.KhoHangs.DeleteOnSubmit(model);
            db.SubmitChanges();
            return RedirectToAction("Index");
        }

        //
        // POST: /KhoHang/Delete/5
        [Authorize(Roles = "0")]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
